<?php

/**
 * sfWidgetFormEmptySelect.
 *
 * @package    scart
 * @subpackage actions
 */
class sfWidgetFormEmptySelect extends sfWidgetFormSelect
{
  protected function configure($options = array(), $attributes = array())
  {
    parent::configure($options, $attributes);
    $this->addOption('add_empty', false);
  }
  
  public function render($name, $value = null, $attributes = array(), $errors = array())
  {
    if ($this->getOption('multiple'))
    {
      $attributes['multiple'] = 'multiple';

      if ('[]' != substr($name, -2))
      {
        $name .= '[]';
      }
    }
    
    $choice = array();
    if (false !== $this->getOption('add_empty'))
    {
      $choice[''] = true === $this->getOption('add_empty') ? '' : $this->getOption('add_empty');
    }
    
    $choices = $this->getOption('choices');
    if ($choices instanceof sfCallable)
    {
      $choices = $choices->call();
    }
    
    $choices = array_merge($choice, $choices);

    return $this->renderContentTag('select', "\n".implode("\n", $this->getOptionsForSelect($value, $choices))."\n", array_merge(array('name' => $name), $attributes));
  }
}
